<!doctype html>
<html lang="zh-Hans-CN">

<head>
    <meta charset="utf-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0, viewport-fit=cover">
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <title>待支付</title>
    <link rel="stylesheet" href="/ui/mobile/iconfont/iconfont.css">
    <link rel="stylesheet" href="/ui/mobile/css/ui.all.css"/>
    <link rel="stylesheet" href="/ui/mobile/css/style.css" type="text/css"/>
</head>

<body>
<main class="c-main order-pay-main">
    <div data-wrap="orderInfo"></div>
    <div class="fixed-bs-box">
        <div class="fixed-bs order-fixed-bot">
            <p class="bot-price">合计：<span data-name="price"></span></p>
            <a data-btn="submit" href="javascript:" class="bot-btn">立即支付</a>
        </div>
    </div>
</main>
<script type="text/javascript" src="/m/public/all.ui.js"></script>
<script src="/ui/mobile/iconfont/iconfont.js"></script>
<script src="/m/public/util.js"></script>

<script src="/m/questionOrder/js/getOrderInfoWidget.js"></script>
<script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>

<script type="text/javascript">
    const {orderId} = $.getPageParam();

    if (!orderId) {
        location.href = "/m/error.html?" + $.param({msg: "订单ID不能为空", back: window.location.href});
    }

    getQuesOrderDetails({orderId}, ({Code, Message, Result: data}) => {
        if (Code !== 10000) {
            location.href = "/m/error.html?" + $.param({msg: Message, back: window.location.href});
        }

        const {price, orderState} = data;

        if (orderState !== 1) {
            location.href = "/m/questionOrder/paySuccess.html?" + $.param({orderId});
        }

        const $orderInfoWidget = getOrderInfoWidget(data);
        $('[data-wrap="orderInfo"]').replaceWith($orderInfoWidget);

        $('[data-name="price"]').text("¥" + (price / 100).toFixed(2));

        $('[data-btn="submit"]').on("click", () => {
            //进行支付
            addQuesWxUnifiedOrder({orderId}, ({Code, Message, Result: data}) => {
                if (Code !== 10000) {
                    return $.toast(Message);
                }

                WeixinJSBridge.invoke("getBrandWCPayRequest", data, function (res) {
                    if (res.err_msg === "get_brand_wcpay_request:ok") {
                        location.href = "/m/questionOrder/orderResult.html?" + $.param({orderId});
                    }
                })
            });
        });

    });

    function addQuesWxUnifiedOrder(param, callback = Function()) {
        return $.sApi({
            data: param,
            url: "/api/QuestionWxPay/addQuesWxUnifiedOrder"
        }, callback)
    }

    function getQuesOrderDetails(param, callback = Function()) {
        return $.sApi({
            data: param,
            url: "/api/QuestionOrder/getQuesOrderDetails"
        }, callback)
    }

</script>

</body>
</html>